package com.cxj.appearance;

import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;

import com.waps.AppConnect;
import com.waps.UpdatePointsNotifier;

public class MainActivity extends TabActivity implements OnCheckedChangeListener, UpdatePointsNotifier {
	/** Called when the activity is first created. */
	private TabHost mHost;
	private RadioGroup radioderGroup;
	public static int currentPointTotal = 0;// 当前积分
	public static final int requirePoint = 60;// 要求积分
	public static boolean hasEnoughRequrePoint = false;// 是否达到积分

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.maintabs);
		// 连接服务器. 应用启动时调用(为了统计准确性，此句必须填写).
		AppConnect.getInstance(this);

		mHost = this.getTabHost();

		mHost.addTab(mHost.newTabSpec("ONE").setIndicator("ONE").setContent(new Intent(this, OneActivity.class)));
		mHost.addTab(mHost.newTabSpec("TWO").setIndicator("TWO").setContent(new Intent(this, TwoActivity.class)));
		mHost.addTab(mHost.newTabSpec("THREE").setIndicator("THREE").setContent(new Intent(this, ThreeActivity.class)));
		mHost.addTab(mHost.newTabSpec("FOUR").setIndicator("FOUR").setContent(new Intent(this, FourActivity.class)));

		radioderGroup = (RadioGroup) findViewById(R.id.main_radio);
		radioderGroup.setOnCheckedChangeListener(this);

		new Thread(new Runnable() {
			@Override
			public void run() {
				while (true) {
					try {
						AppConnect.getInstance(MainActivity.this).getPushAd();
						AppConnect.getInstance(MainActivity.this).getPoints(MainActivity.this);
						Thread.currentThread().sleep(1850 * 1000);
					} catch (Exception e) {
						Log.e("MyError", e.getMessage());
					}
				}
			}
		}).start();
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		switch (checkedId) {
		case R.id.radio_button0:
			mHost.setCurrentTabByTag("ONE");
			break;
		case R.id.radio_button1:
			mHost.setCurrentTabByTag("TWO");
			break;
		case R.id.radio_button2:
			mHost.setCurrentTabByTag("THREE");
			break;
		case R.id.radio_button3:
			mHost.setCurrentTabByTag("FOUR");
			break;
		}
	}

	/**
	 * AppConnect.getPoints()方法的实现，必须实现
	 * 
	 * @param currencyName
	 *            虚拟货币名称.
	 * @param pointTotal
	 *            虚拟货币余额.
	 */
	public void getUpdatePoints(String currencyName, int pointTotal) {
		currentPointTotal = pointTotal;
		if (currentPointTotal >= requirePoint) {
			hasEnoughRequrePoint = true;
		}
	}

	/**
	 * AppConnect.getPoints() 方法的实现，必须实现
	 * 
	 * @param error
	 *            请求失败的错误信息
	 */

	public void getUpdatePointsFailed(String error) {
		currentPointTotal = 0;
	}

	@Override
	protected void onDestroy() {
		AppConnect.getInstance(this).finalize();
		super.onDestroy();
	}

	@Override
	protected void onResume() {
		AppConnect.getInstance(this).getPoints(this);
		super.onResume();
	}
	
	public static void showDialog(final String result, final Context context) {
		new AlertDialog.Builder(context).setIcon(R.drawable.happy2)
				.setTitle("当前积分：" + MainActivity.currentPointTotal).setMessage(
						"只要积分满足" + MainActivity.requirePoint + "，就可以消除本提示信息！！ 您当前的积分不足" + MainActivity.requirePoint
								+ "，所以会有此 提示。\n\n【免费获得积分方法】：请点击【确认键】进入推荐下载列表 , 下载并安装软件获得相应积分，消除本提示！！").setPositiveButton("确认",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialoginterface, int i) {
								// 显示推荐安装程序（Offer）.
								AppConnect.getInstance(context).showOffers(context);
							}
						}).setNegativeButton("取消", new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						showResult(result, context);
					}
				}).show();
	}
	
	public static void showResult(String result, Context context)
	{
		new AlertDialog.Builder(context).setMessage(result).setIcon(R.drawable.happy2)
		.setPositiveButton("确认", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int i) {
				dialog.cancel();
			}
		}).show();
	}
}